﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;

namespace VentaPorCatalogos.UserControls
{
    public enum Selector
    {
        Primera,
        Anterior,
        Siguiente,
        Ultima,
        Actual
    }

    public class PaginadorEventArgs: EventArgs
    {
        public Selector Comando { get; set; }
    }

    public partial class Paginador : System.Web.UI.UserControl
    {
        public delegate void ComandoDelegate(object sender, PaginadorEventArgs e);
        [Browsable(true)]
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        public event ComandoDelegate ComandoPagina;

        public int Pagina
        {
            get
            {
                int wPagina = 0;
                int.TryParse(lblPagina.Text, out wPagina);
                if (wPagina > 0)
                    return wPagina - 1;
                else
                    return 0;
            }
            set
            { lblPagina.Text = value.ToString(); }
        }

        public string Paginas
        {
            get
            { return lblPaginas.Text; }
            set
            { lblPaginas.Text = value; }
        }



        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void picPrimera_Click(object sender, ImageClickEventArgs e)
        {
            if (ComandoPagina != null)
                ComandoPagina(this, new PaginadorEventArgs() { Comando = Selector.Primera });
        }

        protected void picAnterior_Click(object sender, ImageClickEventArgs e)
        {
            if (ComandoPagina != null)
                ComandoPagina(this, new PaginadorEventArgs() { Comando = Selector.Anterior });

        }

        protected void picSiguiente_Click(object sender, ImageClickEventArgs e)
        {
            if (ComandoPagina != null)
                ComandoPagina(this, new PaginadorEventArgs() { Comando = Selector.Siguiente });

        }

        protected void picUltima_Click(object sender, ImageClickEventArgs e)
        {
            if (ComandoPagina != null)
                ComandoPagina(this, new PaginadorEventArgs() { Comando = Selector.Ultima });

        }


        public void CompPaginador(Selector pnSelector)
        {
            switch (pnSelector)
            {
                case Selector.Primera:
                    lblPagina.Text = "1";
                    break;
                case Selector.Anterior:
                    if (Convert.ToInt32(lblPagina.Text) > 1)
                    {
                        lblPagina.Text = (Convert.ToInt32(lblPagina.Text) - 1).ToString();
                    }
                    break;
                case Selector.Siguiente:
                    if (Convert.ToInt32(lblPagina.Text) < Convert.ToInt32(lblPaginas.Text))
                    {
                        lblPagina.Text = (Convert.ToInt32(lblPagina.Text) + 1).ToString();
                    }
                    break;
                case Selector.Ultima:
                    lblPagina.Text = lblPaginas.Text;
                    //picUltima.Visible = False
                    break;
                default:
                    //lblPagina.Text = Session["lblPagina"].ToString();
                    //lblPaginas.Text = Session["lblPaginas"].ToString();
                    break;
            }

        }


        public void PaginadorMethod(int pTotalRegistros, Selector pnSelector, Int32 pPageSize)
        {
            if (pTotalRegistros > 0)
            {
                lblPaginas.Text = Convert.ToString((pTotalRegistros / pPageSize) + 1);
                Session["lblPaginas"] = lblPaginas.Text;
            }
            else
            {
                lblPaginas.Text = "0";
            }

            picPrimera.Enabled = ((pnSelector != Selector.Primera) || (Pagina != 0));
            picAnterior.Enabled = (Pagina > 0);
            picSiguiente.Enabled = ((Pagina + 1) < Convert.ToInt64(lblPaginas.Text));
            picUltima.Enabled = pnSelector != Selector.Ultima || (Pagina + 1) == Convert.ToInt64(lblPaginas.Text);
        }


        
    }
}